package com.task.web.controller.core;

import javax.annotation.Resource;

import com.task.core.domain.AdvertiserInfo;
import com.task.core.domain.AffiliateInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.AdvertiserRequestDTO;
import com.task.core.dto.request.AffiliateRequestDTO;
import com.task.core.service.AdvertiserService;
import com.task.core.service.AffiliateService;
import com.task.core.service.CheckService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/8/14 15:09
 */
@Api("联盟管理")
@RestController
@RequestMapping("/affiliate")
public class AffiliateController extends BaseController {

  @Resource
  private AffiliateService affiliateService;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('alliance:list')")
  @GetMapping("/getAffiliateByPage")
  public TableDataInfo getAffiliateByPage(AffiliateRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(affiliateService.getAffiliateByPage(request));
  }


  @PreAuthorize("@ss.hasPermi('alliance:option')")
  @GetMapping("/getAffiliateForOption")
  public TableDataInfo getAffiliateForOption(AffiliateRequestDTO request) {
    return getDataTable(affiliateService.getAffiliateForOption(request));
  }

  @Log(title = "联盟管理", businessType = BusinessType.INSERT)
  @PostMapping("/addAffiliate")
  public AjaxResult addAffiliate(@RequestBody AffiliateRequestDTO request) {
    affiliateService.addAffiliate(request);
    return success();
  }

  @GetMapping("/getAffiliateByCode/{affiliateCode}")
  @PreAuthorize("@ss.hasPermi('system:alliance:edit')")
  public AjaxResult getAffiliateByCode(@PathVariable String affiliateCode) {
    checkService.check("data_affiliate", affiliateCode);
    AffiliateInfo advertiserInfo = affiliateService.getAffiliateByCode(affiliateCode);
    return success(advertiserInfo);
  }

  @Log(title = "联盟管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyAffiliate")
  public AjaxResult modifyAffiliate(@RequestBody AffiliateRequestDTO request) {
    checkService.check("data_affiliate", request.getCode());
    affiliateService.modifyAffiliate(request);
    return success();
  }

  @Log(title = "联盟管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteAffiliate/{affiliateCode}")
  public AjaxResult deleteAffiliate(@PathVariable String affiliateCode) {
    checkService.check("data_affiliate", affiliateCode);
    affiliateService.deleteAffiliate(affiliateCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }

}
